package com.asura.bootdubbo.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.asura.ecm.bean.OrderInfo;
import com.asura.ecm.service.OrderService;
import com.asura.ecm.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 1、将服务提供者注册到注册中心（暴露服务）
 * 1）、导入dubbo依赖（2.6.2）\操作zookeeper的客户端(curator)
 * 2）、配置服务提供者
 * <p>
 * 2、让服务消费者去注册中心订阅服务提供者的服务地址
 */
@Service
public class OrderServiceImpl implements OrderService {

    //@Autowired
    @Reference(loadbalance = "random", timeout = 1000) //dubbo直连
    UserService userService;

    @HystrixCommand(fallbackMethod = "initOrderFallback")
    @Override
    public List<OrderInfo> initOrder(String userId) {
        List<OrderInfo> orderList = userService.getUserOrderList(userId);
        return orderList;
    }

    public List<OrderInfo> initOrderFallback(String userId) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setId(1L);
        orderInfo.setName("大西瓜");
        return Arrays.asList(orderInfo);
    }

}
